<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 22.03.14
 * Time: 15:52
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

define("BASE_DQ_PLUGIN_PATH", dirname(__FILE__) . "/");

require(realpath(dirname(__FILE__) . "../../../../") . '/wp-blog-header.php');
require_once BASE_DQ_PLUGIN_PATH . "classes/QuizDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/QuestionDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/QuizResultChecker.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/MarkConverter.php";

$quizDao = new QuizDao();
$questionDao = new QuestionDao();
$checker = new QuizResultChecker();

if (!isset($_REQUEST["id"])
    || intval($_REQUEST["id"]) <= 0
    || !$quizDao->getById(intval($_REQUEST["id"]))
) {
    header("Location: /");
}

$quiz = $quizDao->getById(intval($_GET["id"]));
$studentId = isset($_REQUEST["studentId"]) ?
    intval($_REQUEST["studentId"]) : wp_get_current_user()->ID;
$quizResult = $checker->check($quiz->id, $studentId);

// Fix for page title
$wp_query->is_404 = false;
?>

<?php get_header(); ?>

<? include_once BASE_DQ_PLUGIN_PATH . "header.php"; ?>

    <div id="content" class="left">
        <h3>Результаты теста</h3>

        <h2 class="entry-title">
            <b>"<?= $quiz->name ?>"</b>
        </h2>

        <table class="quiz-result-table">
            <tbody>
            <tr>
                <td>Количество вопросов:</td>
                <td><?= $quizResult->maxScore ?></td>
            </tr>
            <tr>
                <td>Количество верных ответов:</td>
                <td><?= count($quizResult->finishedQuestions) ?></td>
            </tr>
            <tr>
                <td>Балы, (текущий/максимальный):</td>
                <td><?= $quizResult->score . " / " . $quizResult->maxScore ?></td>
            </tr>
            <tr>
                <td colspan="2">Оценки</td>
            </tr>
            <tr>
                <td>5-бальная система:</td>
                <td><?=
                    MarkConverter::to_5_point(
                        $quizResult->score,
                        $quizResult->maxScore
                    ) ?></td>
            </tr>
            <tr>
                <td>100-бальная система:</td>
                <td><?=
                    MarkConverter::to_100_point(
                        $quizResult->score,
                        $quizResult->maxScore
                    ) ?>
                </td>
            </tr>
            <tr>
                <td>Международная система оценивания:</td>
                <td>
                    <?=
                    MarkConverter::to_ABCDEF_point(
                        $quizResult->score,
                        $quizResult->maxScore
                    ) ?>
                </td>
            </tr>
            </tbody>
        </table>

        <a id="show-quiz-results-details" href="javascript:void(0)">Подробнее о результатах...</a>
        <br>
        <br>

        <table id="quiz-results-details" style="display: none;">
            <tbody>
            <? $questions = $questionDao->getAllByQuizId($quiz->id) ?>
            <? foreach ($questions as $question) { ?>
                <tr>
                    <td><?= $question->content ?></td>
                    <td>
                        <? if (in_array($question->id, $quizResult->finishedQuestions)) { ?>
                            <span class="correct-ans">Верно</span>
                        <? } else { ?>
                            <span class="incorrect-ans">Не верно</span>
                        <? } ?>
                    </td>
                </tr>
            <? } ?>
            </tbody>
        </table>
    </div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>